package com.evil.common.application.enums.order;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 订单来源
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum OrderSourceTypeEnum {

    /**
     * 订单来源（0-新建订单，1-导入订单，2-机器人创建, 3-订单复制）
     */
    NEW_ORDER(0, "新建订单"),
    IMPORT_ORDER(1, "导入订单"),
    ROBOT_ADD_ORDER(2, "机器人创建"),
    ORDER_COPY(3, "订单复制"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    /**
     * 机器人创建
     *
     * @return boolean
     */
    public boolean isRobot() {
        return ROBOT_ADD_ORDER.id == this.id;
    }

    public static Optional<OrderSourceTypeEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static OrderSourceTypeEnum findById(Integer id) {
        return OrderSourceTypeEnum.isExistById(id).orElseThrow(() ->
                new BusinessException(BaseEnum.ERROR_PARAMETER, "订单来源参数错误"));
    }
}
